package demo.java.parallelprogram.queue;

public class SyncAccount{

	private String  name;
	private float amount;
	
	public SyncAccount(String name,float amount){
		this.name=name;
		this.amount=amount;
	}
	
	//存钱
	public void deposit(float amt){
		amount+=amt;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	//取钱
	public void withdraw(float amt){
		amount-=amt;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public float getBalance(){
		return amount;
	}
	

	public static void main(String[] args) {
		SyncAccount account = new SyncAccount("zhang san" , 10000.0f);
		AccountOperator accountOperator = new AccountOperator(account);
		
		Thread threads[] = new Thread[5];
		for(int i=0;i<5;i++){
			threads[i]=new  Thread(accountOperator,"Thread:"+i);
			threads[i].start();
		}
	}
	
	
}

class AccountOperator implements Runnable{

	private SyncAccount account;
	
	
	public AccountOperator(SyncAccount account){
		this.account=account;
	}
	
	
	@Override
	public  void run() {
		synchronized (account) {
			account.deposit(500);
			account.withdraw(500);
			System.out.println(Thread.currentThread().getName());
		}
	}
}
